//
//  YDAddLunchVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/5/26.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit
import Toaster
class YDAddLunchVC: UIViewController {

    @IBOutlet weak var timeTF: UITextField!//勾餐时间
    @IBOutlet weak var typeATF: UITextField!//A套餐
    @IBOutlet weak var typeBTF: UITextField!//B套餐
    
    @IBOutlet weak var datePickerView: UIDatePicker!
    
    @IBOutlet weak var datepickerBgviwe: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        datePickerView.locale = NSLocale(localeIdentifier: "zh_CN") as Locale
        //显示方式是只显示年月日
        datePickerView.datePickerMode = .date
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //时间修改成功
    @IBAction func datepickerChange(_ sender: Any) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let date = datePickerView.date
        let dateText = dateFormatter.string(from: date)
        timeTF.text = dateText
    }
    //确定时间
    @IBAction func ensureChoose(_ sender: Any) {
        datepickerBgviwe.isHidden = true
    }
    //取消选择时间
    @IBAction func cancelChoose(_ sender: Any) {
        datepickerBgviwe.isHidden = true
        timeTF.text = ""
    }
    //选择勾餐时间
    @IBAction func choseTimeAction(_ sender: Any) {
        datepickerBgviwe.isHidden = false
    }
    
    //关闭
    @IBAction func colose(_ sender: Any) {
        
        self.dismiss(animated: true) { 
            
        }
        
    }
    
    
    //确认
    @IBAction func sureBtnclick(_ sender: Any) {
        if  timeTF.text == ""   ||
            typeBTF.text == nil ||
            typeBTF.text == ""  ||
            typeATF.text == nil ||
            typeATF.text == ""{
            Toast(text: "请填写完整信息", delay:0, duration: 1.5).show()
            return
        }
        let userInfo = UserDefaults.standard.object(forKey: USERINFO) as? [String:AnyObject]
        let token = "token" //userInfo!["token"] as! String
        let id = userInfo!["id"] 
        let params = [
            "token":token,
            "id":id,
            "time":timeTF.text ?? "",
            "a_can":typeATF.text ?? "",
            "b_can":typeBTF.text ?? "",
            ] as [String : Any]
        
        YMNetworkManager().postRequset(urlString: PUSH_WORKING_LUNCH, params: params, success: { (responser) in
            YMLog.log(item: responser)
            if responser["status"]?.int8Value == 1{
                 Toast(text: "发布成功", delay:0, duration: 1.5).show()
                self.dismiss(animated: true, completion: {
                    
                })
            }else{
                 Toast(text: "发布失败", delay:0, duration: 1.5).show()
            }
        }) { (error) in
            
        }
        
    }
    

}

extension YDAddLunchVC : UITextFieldDelegate {

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        return true
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
}
